package com.eam.eventhandler;

import com.eam.data.EamMaintainPlan;
import org.openbravo.base.exception.OBException;
import org.openbravo.base.model.Entity;
import org.openbravo.base.model.ModelProvider;
import org.openbravo.base.model.Property;
import org.openbravo.client.kernel.event.EntityDeleteEvent;
import org.openbravo.client.kernel.event.EntityNewEvent;
import org.openbravo.client.kernel.event.EntityPersistenceEventObserver;
import org.openbravo.client.kernel.event.EntityUpdateEvent;
import org.openbravo.dal.core.OBContext;
import org.openbravo.dal.service.OBDal;
import org.openbravo.database.ConnectionProvider;
import org.openbravo.database.SessionInfo;
import org.openbravo.erpCommon.utility.Utility;
import org.openbravo.service.db.DalConnectionProvider;
import javax.enterprise.event.Observes;
import java.util.Calendar;
import java.util.Date;

public class MaintainPlanEventHandler extends EntityPersistenceEventObserver {
	private static Entity[] entities = { ModelProvider.getInstance().getEntity(EamMaintainPlan.ENTITY_NAME) };

	@Override
	protected Entity[] getObservedEntities() {
		return entities;
	}
	public void onUpdate(@Observes EntityUpdateEvent event) throws Throwable {
		if (!isValidEvent(event)) {
			return;
		}
		
		//获取计划
		EamMaintainPlan plan = (EamMaintainPlan) event.getTargetInstance();
		
		// 将要更改的实体类
		final Entity detailsEntity = ModelProvider.getInstance().getEntity(EamMaintainPlan.ENTITY_NAME);
		final Property endtime = detailsEntity.getProperty(EamMaintainPlan.PROPERTY_PLANENDTIME);
		final Property lengthtime = detailsEntity.getProperty(EamMaintainPlan.PROPERTY_LENGTHTIME);
		
		String tabId = SessionInfo.getProcessId();
		
		if(tabId == null){
			return ;
		}
		
		if(tabId.equalsIgnoreCase("DC44A0CD66174A5AA8B75F4225F32BA6")){
		
		Date sDate = plan.getPlanStateTime(); //计划开始时间
		//Date eDate = plan.getPlanEndTime(); //计划结束时间
		String mold = plan.getPlanType();    //该计划的保养周期是周还说月还是季度或者年
		
		if(sDate == null){
			return ;
		}
		
		int cycles = 0;
		
		if(mold.equals("week")){
			cycles = 7;
		}else if(mold.equals("month")){
			cycles = 30 ;
		}else if(mold.equals("quarter")){
			cycles = 90;
		}else if(mold.equals("year")){
			cycles = 365 ;
		}
		
		
		//根据计划开始时间自动算出计划结束时间并存入表中
        Calendar ca = Calendar.getInstance();
		
		ca.setTime(sDate);
		
		ca.add(Calendar.DATE, cycles);// cycles为增加的天数
		
		Date eDate = ca.getTime(); // 计划结束时间
		
		
		event.setCurrentState(endtime, eDate);
		
		long longtimes =  (eDate.getTime() - sDate.getTime())/3600000;
		String longtime =String.valueOf(longtimes);
		event.setCurrentState(lengthtime, longtime);
		
		/*if(eDate == null){
			String language = OBContext.getOBContext().getLanguage().getLanguage();
	        ConnectionProvider conn = new DalConnectionProvider(false);
	        throw new OBException(Utility.messageBD(conn, "请指定计划结束时间!", language));
		}*/
		
		if(sDate != null){
			if(sDate.after(eDate)){
				String language = OBContext.getOBContext().getLanguage().getLanguage();
		        ConnectionProvider conn = new DalConnectionProvider(false);
		        throw new OBException(Utility.messageBD(conn, "计划开始时间晚于计划结束时间，请仔细检查", language));
			}
		}
		
		if(eDate.before(new Date())){
			String language = OBContext.getOBContext().getLanguage().getLanguage();
	        ConnectionProvider conn = new DalConnectionProvider(false);
	        throw new OBException(Utility.messageBD(conn, "计划结束时间早于当前时间，请仔细检查", language));
		}
		}
		
	}
	
	
	public void onSave(@Observes EntityNewEvent event) {
		if (!isValidEvent(event)) {
			return;
		}
		
		//获取计划
		EamMaintainPlan plan = (EamMaintainPlan) event.getTargetInstance();
		
		// 将要更改的实体类
		final Entity detailsEntity = ModelProvider.getInstance().getEntity(EamMaintainPlan.ENTITY_NAME);
		final Property plantype = detailsEntity.getProperty(EamMaintainPlan.PROPERTY_DISTINGUISH);
		final Property lengthtime = detailsEntity.getProperty(EamMaintainPlan.PROPERTY_LENGTHTIME);
		final Property endtime = detailsEntity.getProperty(EamMaintainPlan.PROPERTY_PLANENDTIME);
		
		
		Date sDate = plan.getPlanStateTime(); //计划开始时间
		String mold = plan.getPlanType();    //该计划的保养周期是周还说月还是季度或者年
		//Date eDate = plan.getPlanEndTime(); //计划结束时间
		
		int cycles = 0;
		
		if(mold.equals("week")){
			cycles = 7;
		}else if(mold.equals("month")){
			cycles = 30 ;
		}else if(mold.equals("quarter")){
			cycles = 90;
		}else if(mold.equals("year")){
			cycles = 365 ;
		}
		
		
		//根据计划开始时间自动算出计划结束时间并存入表中
        Calendar ca = Calendar.getInstance();
		
		ca.setTime(sDate);
		
		ca.add(Calendar.DATE, cycles);// cycles为增加的天数
		
		Date eDate = ca.getTime(); // 计划结束时间
		
		event.setCurrentState(plantype, "MAINTAIN");
		event.setCurrentState(endtime, eDate);
		OBDal.getInstance().save(plan);
		
		if(sDate == null && eDate == null){
			return ;
		}
		long longtimes =  (eDate.getTime() - sDate.getTime())/3600000;
		String longtime =String.valueOf(longtimes);
		event.setCurrentState(lengthtime, longtime);
		
		if(sDate != null && eDate != null){
			if(sDate.after(eDate)){
				String language = OBContext.getOBContext().getLanguage().getLanguage();
		        ConnectionProvider conn = new DalConnectionProvider(false);
		        throw new OBException(Utility.messageBD(conn, "计划开始时间晚于计划结束时间，请仔细检查", language));
			}
		}
		
		if(eDate != null && eDate.before(new Date())){
			String language = OBContext.getOBContext().getLanguage().getLanguage();
	        ConnectionProvider conn = new DalConnectionProvider(false);
	        throw new OBException(Utility.messageBD(conn, "计划结束时间早于当前时间，请仔细检查", language));
		}
		
		
	}
	
	public void onDelete(@Observes
	EntityDeleteEvent event) {
		if (!isValidEvent(event)) {
			return;
		}
	}
	
}
